package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学生
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result list(StudentQueryParam studentQueryParam){
        log.info("请求参数： {}", studentQueryParam);
        PageBean pageBean =  studentService.list(studentQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 新增学生
     * @param student
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("请求参数： {}", student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("请求参数： {}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("请求参数： {}", id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("请求参数： {}", student);
        studentService.update(student);
        return Result.success();
    }


    /**
     * 根据id修改学生分数
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateScore(@PathVariable Integer id, @PathVariable Integer score){
        log.info("请求参数： {},{}", id,score);
        studentService.updateScore(id, score);
        return Result.success();
    }

}
